package com.ruoyi.auth.controller;

import com.ruoyi.auth.form.LoginBody;
import com.ruoyi.auth.service.InternetUserLoginService;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.LoginUserWXVO;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * token 控制
 *
 * @author ruoyi
 */
@Api("微信登录")
@RestController
public class WxTokenController {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;


    @Autowired
    private RemoteUserService remoteUserService;


    @Resource
    private InternetUserLoginService internetUserLoginService;

    @ApiOperation(value = "微信用户登录", notes = "微信用户登录")
    @PostMapping("/wxLogin")
    public AjaxResult login(@RequestBody LoginBody form) {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        userInfo.setUserid(userInfo.getSysUser().getUserId());
        userInfo.setUsername(userInfo.getSysUser().getUserName());
        Map<String, Object> map = new HashMap<String, Object>();
        // 获取登录token //
        map.put("token", tokenService.createWxToken(userInfo));
        map.put("userInfo", remoteUserService.getUserInfo(form.getUsername()).getData().getSysUser());
        return AjaxResult.success(map);
    }

    @ApiOperation(value = "微信小程序授权登录", notes = "微信小程序授权登录")
    @ResponseBody
    @PostMapping("/wxApplet/login")
    public AjaxResult loginApplet(@RequestBody Map<String,Object> request) {
        // LoginUser
        //LoginUser userInfo = sysLoginService.loginApplet(code,rawData,signature,encrypteData,iv);

        String code = (String)request.get("code");
        String rawData = (String)request.get("rawData");
        String signature = (String)request.get("signature");
        String encrypteData = (String)request.get("encrypteData");
        String iv = (String)request.get("iv");
        LoginUserWXVO userInfo = sysLoginService.loginApplet(code,rawData,signature,encrypteData,iv);
        //            生成自定义登录态,生成令牌
        // 生成令牌
        //Map<String,Object> result = new HashMap<>();
        //result.put(com.alibaba.nacos.api.common.Constants.TOKEN, tokenService.createWxAppletToken(userInfo));
//        result.put("userInfo",remoteUserService.getUserInfo(userInfo.getUsername()).getData().getSysUser());
        return AjaxResult.success(userInfo);

        // 用户登录
//        LoginUser userInfo = sysLoginService.loginApplet(String code);
//        userInfo.setUserid(userInfo.getSysUser().getUserId());
//        userInfo.setUsername(userInfo.getSysUser().getUserName());
//        Map<String, Object> map = new HashMap<String, Object>();
//        // 获取登录token //
//        map.put("token", tokenService.createWxToken(userInfo));
//        map.put("userInfo", remoteUserService.getUserInfo(form.getUsername()).getData().getSysUser());
//        return AjaxResult.success(map);
    }

    @ApiOperation(value = "微信小程序绑定", notes = "微信小程序绑定")
    @PostMapping("/wxApplet/bind")
    public AjaxResult bindApplet(@RequestBody Map<String,Object> request){
        String password = (String)request.get("password");
        String phoneNumber = (String)request.get("phoneNumber");
        String username = (String)request.get("username");
        LoginUserWXVO userInfo = sysLoginService.bindApplet(username, password,phoneNumber);
//        userInfo.setUserid(userInfo.getSysUser().getUserId());
//        userInfo.setUsername(userInfo.getSysUser().getUserName());
//        Map<String, Object> map = new HashMap<String, Object>();
//        // 获取登录token //
//        map.put("token", tokenService.createWxToken(userInfo));
//        map.put("userInfo", remoteUserService.getUserInfo(form.getUsername()).getData().getSysUser());
        return AjaxResult.success(userInfo);
    }

//    /**
//     * 产业互联网用户登录
//     * @param form
//     * @return
//     */
//    @ApiOperation(value="产业互联网用户登录", notes="产业互联网用户登录")
//    @PostMapping("/intUserLogin")
//    public R<?> intUserLogin(@RequestBody LoginBody form){
//        return internetUserLoginService.login(form.getUsername(), form.getPassword());
//    }

    /**
     * 产业互联网用户手机验证码登录
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
//    @ApiOperation(value="产业互联网用户手机验证码登录", notes="产业互联网用户手机验证码登录")
//    @PostMapping("/phoneLogin")
//    public R<?> phoneLogin(String phone,String code){
//        return internetUserLoginService.phoneLogin(phone, code);
//    }
//
//    @ApiOperation(value="用户退出", notes="用户退出")
//    @DeleteMapping("logout")
//    public R<?> logout(HttpServletRequest request) {
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(loginUser)) {
//            String username = loginUser.getUsername();
//            // 删除用户缓存记录
//            tokenService.delLoginUser(loginUser.getToken());
//            // 记录用户退出日志
//            sysLoginService.logout(username);
//        }
//        return R.ok();
//    }
//
//    @PostMapping("refresh")
//    public R<?> refresh(HttpServletRequest request) {
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(loginUser)) {
//            // 刷新令牌有效期
//            tokenService.refreshToken(loginUser);
//            return R.ok();
//        }
//        return R.ok();
//    }
}
